import 'package:flutter/material.dart';
import 'date_formatter.dart';

/// 日期格式化示例类
class DateFormatSample extends StatelessWidget {
  const DateFormatSample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 当前日期时间
    final now = DateTime.now();
    // 昨天的日期时间
    final yesterday = now.subtract(const Duration(days: 1));
    // 上个月的日期时间
    final lastMonth = now.subtract(const Duration(days: 30));
    // 去年的日期时间
    final lastYear = now.subtract(const Duration(days: 365));

    return Scaffold(
      appBar: AppBar(
        title: const Text('日期格式化示例'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('当前日期: ${DateFormatter.dateFormat(now)}'),
            const SizedBox(height: 8),
            Text('当前日期时间: ${DateFormatter.dateTimeFormat(now)}'),
            const SizedBox(height: 8),
            Text('自定义格式(MM/dd/yyyy): ${DateFormatter.customFormat(now, 'MM/dd/yyyy')}'),
            const SizedBox(height: 8),
            Text('昨天的相对时间: ${DateFormatter.relativeTimeFormat(yesterday)}'),
            const SizedBox(height: 8),
            Text('上个月的相对时间: ${DateFormatter.relativeTimeFormat(lastMonth)}'),
            const SizedBox(height: 8),
            Text('去年的相对时间: ${DateFormatter.relativeTimeFormat(lastYear)}'),
            const SizedBox(height: 16),
            
            const Text('字符串解析为日期:', style: TextStyle(fontWeight: FontWeight.bold)),
            const SizedBox(height: 8),
            Builder(
              builder: (context) {
                const dateStr = '2023-05-20';
                final parsedDate = DateFormatter.parseDateTime(dateStr);
                return Text(
                  parsedDate != null 
                      ? '解析 "$dateStr" => ${DateFormatter.dateFormat(parsedDate)}'
                      : '无法解析 "$dateStr"'
                );
              }
            ),
          ],
        ),
      ),
    );
  }
} 